<?php

/**
 * DistrictController is the default controller for this application
 */
class DistrictController extends ControllerBase {
	
	/**
	 * Admin District Data Service Object
	 * @var DistrictDataService object
	 */
	private $DistrictDataService;
	public $CityDataService;
	/**
	 * DistrictController Constructor
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set District name
		$this->setPageName ( 'admin_District' );
		
		// Init Authentication class
		$authentication = new Authentication ();
		
		// Only administrator can right
		if ($authentication->isAdministrator () == false) {
			
			// Go to login District
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Login/index';
			CommonUtils::redirect ( $url );
		}
		
		// Get status list of District category
		//$this->smarty->assign ( 'statusList', DistrictEntity::getStatusList() );
		
		// New District Data Service
		$this->DistrictDataService = new DistrictDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing District of the site. Return list of all District category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function indexAction() {
		// View District_category list
		$this->listAction ();
	}
	
	/**
	 * Dispplay List of District_category
	 * landing District of the site. Return list of all District_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function listAction() {
		
		$arrElement = array ();
//		$arrElement ['status'] = DistrictEntity::ACTIVE;
		
		$numRecordPerPage = ITEM_PER_PAGE;
		
		// Pager
		$start = $this->getQuery ( 'start', 0 );
		if ($start == '' || $start < 0) {
			$start = 0;
		}
		
		// Get Totals Templates
		$totals = $this->DistrictDataService->getTotals ( $arrElement );
		
		$itemList = $this->DistrictDataService->getItems ( $arrElement, NULL, $start, $numRecordPerPage );
		
		if ($start > $totals) {
			$start = 0;
		}
		$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Admin/District/Index";
		$pageLink = '';
		if ($totals > $numRecordPerPage) {
			$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
			$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
			$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
		}
		
		$this->smarty->assign ( 'DistrictEntities', $itemList );
		$this->smarty->assign ( 'this', $this );
		$this->smarty->assign ( 'pageLink', $pageLink );
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/District/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Add action is trigger when admin want to add the District_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function addAction() {
		
		$CityDataService = new CityDataService();
		$cityList = $CityDataService->getItems( array() );
		$isError = false;
		$msg = "";
		// Get action method and check it
		$actionForm = $this->getPost ( 'submit', '' );
		if (! empty ( $actionForm )) 

		{
			// Get name field
			$District_name = $this->getPost ( 'District_name', "" );
			$District_name = trim ( $District_name );
			// get city_id
			$City_id=$this->getPost('City_id',"");
			// check
			if (! empty ($District_name ) 
				&& !empty($City_id) ) {
				try {
					
					// Init Authentication class
					$authentication = new Authentication ();
					// Save data into database
					$DistrictEntity = $this->DistrictDataService->beforeCreate ();
					$DistrictEntity->district_name = $District_name;
					$DistrictEntity->city_id = $City_id;
					// test loi o vi tri nay
					//var_dump($DistrictEntity);  die();
					//print_r($DistrictEntity,false); die();
					$DistrictId = $this->DistrictDataService->create ( $DistrictEntity );
					
					// Redirect to edit District
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/District/';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					$isError = true;
					// Ignore to process this case. Only show a message
					$msg = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					
					echo $ex->getMessage ();
				
				}
			} else {
				$isError = true;
				
				$msg = 'Please fill in all required fields';
			}
		}
	
		// Create new token key
		$this->createActionKey ();
		$this->smarty->assign ( 'msg', $msg );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign('cityList', $cityList);
		$this->smarty->assign ( 'District_name', isset ( $District_name ) ? $District_name : "" );
		$this->smarty->assign ( 'City_id', isset ( $City_id) ? $City_id : "" );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/District/add.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Edit action is trigger when admin want to edit the District_category information
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function editAction() {
		$CityDataService = new CityDataService();
		$cityList = $CityDataService->getItems( array() );
		// Error flag
		$isError = false;
		$msg = "";
		
		// Get id of District
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		// If the id of District does not exist, go on to view list District
		if (empty ( $id ) || ! is_numeric ( $id )) {
			$this->listAction ();
			return false;
		}
		
		$id = intval ( $id );
			
		// Get data
		$DistrictEntity = $this->DistrictDataService->getById($id );
		// check data
		if (empty ( $DistrictEntity )) {
			$this->listAction ();
			return false;
		}
		// Get action method and check it
		$actionForm = $this->getPost ( 'submit', '' );
		
		// submit
		if (! empty ( $actionForm )) {
			// Get District name field
			$District_name = $this->getPost ( 'District_name', "" );
			$District_name = trim ( $District_name );
			$DistrictEntity->district_name = $District_name;
			
			// Get City_ID field
			$City_ID = $this->getPost('City_id',"");
			$DistrictEntity->city_id = $City_ID;

			// check
			if (! empty ( $District_name )) {
				try {
					$res = $this->DistrictDataService->update ( $DistrictEntity );
					//var_dump($res);
					$isError = false;
					
					// Show a message
					$msg = 'The record has been updated successfully';
					// Redirect to edit District
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/District/';
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message
					$msg = 'The item cannot update successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} 
			
			else {
				$isError = true;
				
				$msg = 'Please fill in all required fields';
			}
			
		}
		
		// create session
		$this->createActionKey ();
		$this->smarty->assign ( 'msg', $msg );
		$this->smarty->assign ( 'isError', $isError );
		$this->smarty->assign('cityList', $cityList);
		$this->smarty->assign('id',$DistrictEntity->id);
		$this->smarty->assign ( 'District_name', $DistrictEntity->district_name );
		$this->smarty->assign ( 'City_id', $DistrictEntity->city_id);
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/District/edit.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}
	
	/**
	 * Delete action is trigger when administrator want to delete a District_category
	 *
	 * @access public
	 *
	 * @return void
	 */
	public function deleteAction() {
		// Get Id of District
		$id = $this->getPost ( 'idValue', 0 );
		
		if ($this->validateActionKey () && ! empty ( $id ) && is_numeric ( $id )) {
			
			$id = intval ( $id );
			// Delete District
			$res = $this->DistrictDataService->del ( $id );
		}
		$this->listAction ();
	}

}
